<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Traffic extends MY_Controller {

	function index() {
		header("Location: /traffic/bandwidth");
	}

	function bandwidth() {
		$this->load->model('service');
		$this->load->model('unit');
		$this->data['service'] = $this->service->all();
		$user_info = $this->session->userdata('user_info');
		$this->data['unit'] = $this->unit->user_unit($user_info['id']);
		$this->load->view('traffic/bandwidth', array('data' => $this->data));
	}

	function bandwidth_unit() {
		$this->load->model('unit');
		$this->load->model('service');
		$service_id = $this->input->get('service_id', TRUE);
		$user_info = $this->session->userdata('user_info');
		$unit = $this->unit->unit_tree($service_id, $user_info['id']);
		$this->data['unit'] = $unit;
		$this->data['service'] = $this->service->map();
		$this->load->view('traffic/bandwidth_unit', array('data' => $this->data));
	}

	function bandwidth_domain() {
		$this->load->model('unit');
		$this->load->model('unit_bind');
		$this->load->model('cdn_domain');
		$service_id = $this->input->get('service_id', TRUE);
		$unit = $this->unit->service_unit($service_id);
		$domain = array();
		foreach ($unit as $k => $u) {
			$domain_id = $this->unit_bind->bdomain($u['id']);
			$domain_info = array();
			foreach ($domain_id as $v) {
				$domain_info[] = $this->cdn_domain->find($v['domain_id']);
			}
			$unit[$k]['domain'] = $domain_info;
		}
		$this->data['unit'] = $unit;
		$this->load->view('traffic/bandwidth_domain', array('data' => $this->data));
		
	}

	function bandwidth_chart() {
		$this->load->model('unit');
		$user_info = $this->session->userdata('user_info');
		$user_unit = $this->unit->user_unit_array($user_info['id']);
		$cond['start_date'] = $this->input->post('start_date', TRUE);
		$cond['end_date'] = $this->input->post('end_date', TRUE);
		$unit = $this->input->post('unit', TRUE);
		if ($unit === 'all') {
			$cond['unit_id'] = $user_unit;
		} else {
			$cond['unit_id'] = explode('_', $unit);
		}
		
		$this->load->model('unit_traffic');
		$this->data['data'] = $this->unit_traffic->bandwidth($cond);	
		echo json_encode($this->data['data']);
	}

	function flow() {
		$this->load->model('service');
		$this->load->model('unit');
		$this->data['service'] = $this->service->all();
		$user_info = $this->session->userdata('user_info');
		$this->data['unit'] = $this->unit->user_unit($user_info['id']);
		$this->load->view('traffic/flow', array('data' => $this->data));
	}

	function flow_chart() {
		$cond['service_id'] = $this->input->post('service_id', TRUE);
		$cond['start_date'] = $this->input->post('start_date', TRUE);
		$cond['end_date'] = $this->input->post('end_date', TRUE);
		$cond['unit_id'] = $this->input->post('unit_id', TRUE);
		$this->load->model('unit_traffic');
		$this->data['data'] = $this->unit_traffic->flow($cond);	
		echo json_encode($this->data['data']);
	}

	function flow_unit() {
		$this->bandwidth_unit();
	}

	function domain() {
		$this->load->model('service');
		$this->load->model('unit');
		$this->data['service'] = $this->service->all();
		$user_info = $this->session->userdata('user_info');
		$this->data['unit'] = $this->unit->user_unit($user_info['id']);
		$this->load->view('traffic/domain', array('data' => $this->data));
	}

	function domain_chart() {
		$cond['service_id'] = $this->input->post('service_id', TRUE);
		$cond['start_date'] = $this->input->post('start_date', TRUE);
		$cond['end_date'] = $this->input->post('end_date', TRUE);
		$cond['domain_id'] = $this->input->post('domain', TRUE);
		$this->load->model('domain_traffic');
		$this->data['data'] = $this->domain_traffic->get($cond);	
		echo json_encode($this->data['data']);
	}

	function domain_domain() {
		$this->bandwidth_domain();
	}
}
